<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Xml.KumomadoXmlParser');
	/**
	 * @package libraries/language/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoLanguage {
		/**
		 * @var string
		 */
		protected $language;
		/**
		 * @var string
		 */
		protected $default = 'en-us';
		/**
		 * @var array
		 */
		protected $source = array();
		/**
		 * @var string
		 */
		protected $source_file;
		/**
		 * @var string
		 */
		protected $source_package;
		/**
		 * Construct
		 * @param string $source_file[option]
		 * @param string $source_package[option]
		 * @param string $language[option]
		 * @return array
		 */
		public function __construct($source_file = '', $source_package = '', $language = null) {
			if (!defined('KUMOMADO_LANGUAGES_PATH')) {
				throw new KumomadoLanguageException('KUMOMADO_LANGUAGES_PATH is not define.');
			}
			if (isset($_SESSION['language'])) {
				$this->language = $_SESSION['language'];
			} else {
				$this->language = Kumomado::config('main')->Lang;
			}
			if ($source_file != '') {
				return $this->load($source_file, $source_package, $language);
			}
		}
		/**
		 * @param string $source_file
		 * @param string $app[optional]
		 * @param string $sub_app[optional]
		 */
		public function load($source_file, $source_package = '', $language = null) {
			$this->source_file = $source_file;
			$this->source_package = $source_package;
			if (isset($language)) {
				$this->language = $language;
			}
			$this->_parse();
			return $this;
		}
		/**
		 * Parse language source
		 */
		protected function _parse() {
			$xml_parser = new KumomadoXmlParser();
			$source_file = KUMOMADO_LANGUAGES_PATH.$this->language.DIRECTORY_SEPARATOR.$this->source_package.$this->source_file.'.xml';
			if (!file_exists($source_file)) {
				$default_file = KUMOMADO_LANGUAGES_PATH.$this->default.DIRECTORY_SEPARATOR.$this->source_package.$this->source_file.'.xml';
				if (file_exists($default_file)) {
					$source_file = $default_file;
				} else {
					throw new KumomadoLanguageException('Language definition file is not exists!('.$source_file.')');
				}
			}
			$source = $xml_parser->loadFile($source_file);
			if (is_array($source) && is_array($source[0]['childrens'])) {
				foreach ($source[0]['childrens'] as $resource) {
					$this->source[$resource['attributes']['id']] = $resource['nodevalue'];
				}
			}
		}
		/**
		 * Return all language resource
		 * @return array
		 */
		public function source() {
			return $this->source;
		}
		/**
		 * 
		 * @param string $name
		 * @return string
		 */
		public function __get($name) {
			return $this->source[$name];
		}
	}
?>